package com.xx.pmis.web.actions;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Lucky;
import com.xx.pmis.services.LuckyService;
import com.xx.pmis.web.forms.LuckyForm;
import com.xx.pmis.web.util.AccountUtil;
import com.xx.pmis.web.util.CurrentDate;

/**
 * @class 双色球Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class LuckyAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(PresentAction.class);

	LuckyService luckyService = getPMISServices().getLuckyService();
	AccountUtil accountUtil = new AccountUtil();

	// 查询所有双色球
	public ActionForward listLucky(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> all = null;
		try {
			all = luckyService.getAllLucky();
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("luckyList", all);
		request.setAttribute("position", "[物资管理]-[双色球]");
		return mapping.findForward("listLucky");
	}

	// 新增
	public ActionForward addLucky(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("payDate", CurrentDate.currentDate());
		request.setAttribute("year", CurrentDate.currentYear());
		request.setAttribute("position", "[双色球]-[新增]");
		return mapping.findForward("addLucky");
	}

	// 新增，处理方法
	public ActionForward addLucky_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		LuckyForm f = (LuckyForm) form;

		String issueNumber = f.getIssueNumber(); // 期号
		String r1 = f.getR1(); // 红球号码1
		String r2 = f.getR2(); // 红球号码2
		String r3 = f.getR3(); // 红球号码3
		String r4 = f.getR4(); // 红球号码4
		String r5 = f.getR5(); // 红球号码5
		String r6 = f.getR6(); // 红球号码6
		String b1 = f.getB1(); // 蓝球号码1
		String luckyNum = f.getLuckyNum(); // 注数
		String luckyLevel = f.getLuckyLevel(); // 几等奖 1 2 3 4 5 6
		String luckyMoney = f.getLuckyMoney(); // 中奖金额
		String payDate = f.getPayDate(); // 购买日期
		String luckyPay = f.getLuckyPay(); // 结算 0结算，1未结算

		Lucky lucky = new Lucky();
		lucky.setIssueNumber(issueNumber);
		lucky.setR1(r1);
		lucky.setR2(r2);
		lucky.setR3(r3);
		lucky.setR4(r4);
		lucky.setR5(r5);
		lucky.setR6(r6);
		lucky.setB1(b1);
		lucky.setLuckyNum(luckyNum);
		int n = Integer.parseInt(luckyNum);
		// 支付金额，注数*2，每注两元
		lucky.setPayMoney(accountUtil.if1ZeroTo2Zero(String.valueOf(n * 2)));
		lucky.setLuckyLevel(luckyLevel);
		lucky.setLuckyMoney(luckyMoney);
		lucky.setPayDate(payDate);
		lucky.setLuckyPay(luckyPay);

		try {
			luckyService.saveOrUpdateLucky(lucky);
		} catch (Exception e) {
			e.printStackTrace();
		}

		listLucky(mapping, form, request, response);
		return mapping.findForward("listLucky");
	}

	// 编辑
	public ActionForward editLucky(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String luckyID = request.getParameter("luckyID");
		Lucky lucky = null;
		try {
			lucky = luckyService.getLuckyByLuckyID(luckyID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("lucky", lucky);
		request.setAttribute("position", "双色球/编辑");
		return mapping.findForward("editLucky");
	}

	// 编辑，处理方法
	public ActionForward editLucky_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		LuckyForm f = (LuckyForm) form;

		String luckyID = f.getLuckyID();
		String issueNumber = f.getIssueNumber(); // 期号
		String r1 = f.getR1(); // 红球号码1
		String r2 = f.getR2(); // 红球号码2
		String r3 = f.getR3(); // 红球号码3
		String r4 = f.getR4(); // 红球号码4
		String r5 = f.getR5(); // 红球号码5
		String r6 = f.getR6(); // 红球号码6
		String b1 = f.getB1(); // 蓝球号码1
		String luckyNum = f.getLuckyNum(); // 注数
		String luckyLevel = f.getLuckyLevel(); // 几等奖 1 2 3 4 5 6
		String luckyMoney = f.getLuckyMoney(); // 中奖金额
		String payDate = f.getPayDate(); // 购买日期
		String luckyPay = f.getLuckyPay(); // 结算 0结算，1未结算

		Lucky lucky = null;
		try {
			lucky = luckyService.getLuckyByLuckyID(luckyID);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		lucky.setIssueNumber(issueNumber);
		lucky.setR1(r1);
		lucky.setR2(r2);
		lucky.setR3(r3);
		lucky.setR4(r4);
		lucky.setR5(r5);
		lucky.setR6(r6);
		lucky.setB1(b1);
		lucky.setLuckyNum(luckyNum);
		int n = Integer.parseInt(luckyNum);
		// 支付金额，注数*2，每注两元
		lucky.setPayMoney(accountUtil.if1ZeroTo2Zero(String.valueOf(n * 2)));
		lucky.setLuckyLevel(luckyLevel);
		lucky.setLuckyMoney(luckyMoney);
		lucky.setPayDate(payDate);
		lucky.setLuckyPay(luckyPay);
		try {
			luckyService.saveOrUpdateLucky(lucky);
		} catch (Exception e) {
			e.printStackTrace();
		}

		listLucky(mapping, form, request, response);
		return mapping.findForward("listLucky");
	}

	// 删除
	public ActionForward delLucky(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Lucky lucky = null;
		LuckyForm f = (LuckyForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				lucky = luckyService.getLuckyByLuckyID(ids[i]);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				luckyService.deleteLucky(lucky);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		listLucky(mapping, form, request, response);
		return mapping.findForward("listLucky");
	}

	// 结算
	public ActionForward payLucky(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String luckyID = request.getParameter("luckyID");
		Lucky lucky = new Lucky();
		try {
			lucky = luckyService.getLuckyByLuckyID(luckyID);
		} catch (Exception e) {
			log.info("根据双色球ID查询该双色球异常！" + e.toString());
		}

		// 生成记账凭证	
		accountUtil.payExpenseToAccount(lucky.getPayDate(), "旅游", lucky.getLuckyNum(), lucky.getPayMoney(), "购买",
				lucky.getIssueNumber()+ "期彩票");
		// 更新结算状态，结算完成为"0"
		lucky.setLuckyPay("0");
		try {
			luckyService.saveOrUpdateLucky(lucky);
		} catch (Exception e) {
			log.info("保存双色球异常！" + e.toString());
		}

		listLucky(mapping, form, request, response);
		return mapping.findForward("listLucky");
	}

	// 明细
	public ActionForward detailLucky(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String luckyID = request.getParameter("luckyID");

		Lucky lucky = new Lucky();
		try {
			lucky = luckyService.getLuckyByLuckyID(luckyID);
		} catch (Exception e) {
			log.info("根据双色球ID查询该双色球异常！" + e.toString());
		}

		request.setAttribute("lucky", lucky);
		return mapping.findForward("detailLucky");
	}
}